package cn.tth.test.game.mysql;

import lombok.extern.slf4j.Slf4j;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

@Slf4j
public class MysqlUtils {

    //数据库连接地址
    public static String MYSQL_URL="jdbc:mysql://127.0.0.1:3306/test?autoReconnect=true&autoReconnectForPools=true&useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=convertToNull&allowMultiQueries=true&useSSL=false";
    //用户名
    public static String MYSQL_USERNAME= "root";
    //密码
    public static String MYSQL_PASSWORD ="root";//
    //驱动名称
    public static String MYSQL_DRIVER = "com.mysql.jdbc.Driver";


    private static Connection conn = null;

    public static Connection getConnection() {

        try {
            Class.forName(MYSQL_DRIVER);
            conn = DriverManager.getConnection(MYSQL_URL, MYSQL_USERNAME, MYSQL_PASSWORD);

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            log.info(e.getMessage());
            closeConn();
        }
        return conn;
    }

    public static void closeConn(){
        try {
            conn.close();
        } catch (SQLException e) {
            log.info(e.getMessage());
        }
    }

}
